package com.kingdee.eas.custom.aoyuan.aytime.bizbill.app;

import org.apache.log4j.Logger;
import javax.ejb.*;
import java.rmi.RemoteException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import com.kingdee.bos.*;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK;
//import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean;
//import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;

import com.kingdee.eas.framework.app.CoreBillBaseControllerBean;
import com.kingdee.eas.framework.SystemEnum;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.framework.ObjectBaseCollection;
import java.lang.String;
import com.kingdee.eas.framework.CoreBillBaseCollection;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.custom.aoyuan.aytime.TimeCardTypeEnum;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.MachineFactory;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.MachineInfo;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.MachineTypeFactory;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.MachineTypeInfo;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.CardRecordInfo;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.CardRecordSimpleInfo;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.IMachineFetch;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.SimpleCardRecord;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.CardRecordCollection;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.UIContext;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;

public class CardRecordControllerBean extends AbstractCardRecordControllerBean
{
	private static Logger logger =
		Logger.getLogger("com.kingdee.eas.custom.aoyuan.aytime.bizbill.app.CardRecordControllerBean");

	protected IObjectPK _save(Context ctx, IObjectValue model)
			throws BOSException, EASBizException {
		CardRecordInfo saveInfo=(CardRecordInfo)model;
    	Time bizTime=saveInfo.getBizTime();
    	Date bizDate=saveInfo.getBizDate();
    	Calendar c=Calendar.getInstance();
		c.setTime(bizDate);
		c.set(Calendar.HOUR_OF_DAY, bizTime.getHours());
		c.set(Calendar.MINUTE, bizTime.getMinutes());
		c.set(Calendar.SECOND, bizTime.getSeconds());
		Date newBizDate=c.getTime();
		
    	saveInfo.setBizDate(newBizDate);
    	saveInfo.setBizTime(new Time(newBizDate.getTime()));
    	saveInfo.setIpAddr("127.0.0.1");
		return super._save(ctx, model);
	}


	protected IObjectPK _submit(Context ctx, IObjectValue model)
			throws BOSException, EASBizException {
		CardRecordInfo saveInfo=(CardRecordInfo)model;
    	Time bizTime=saveInfo.getBizTime();
    	Date bizDate=saveInfo.getBizDate();
    	Calendar c=Calendar.getInstance();
		c.setTime(bizDate);
		c.set(Calendar.HOUR_OF_DAY, bizTime.getHours());
		c.set(Calendar.MINUTE, bizTime.getMinutes());
		c.set(Calendar.SECOND, bizTime.getSeconds());
		Date newBizDate=c.getTime();
		
    	saveInfo.setBizDate(newBizDate);
    	saveInfo.setBizTime(new Time(newBizDate.getTime()));
    	saveInfo.setIpAddr("127.0.0.1");
		return super._submit(ctx, model);
	}
	
	//    @Override
	protected IObjectCollection _fetchRecords(Context ctx, String machineid,
			Date startDate, Date endDate, boolean isOnline) throws BOSException {
		// TODO Auto-generated method stub
		try
		{
			if (machineid==null)throw(new Exception("请输入考勤机内码!"));
			MachineInfo machine = MachineFactory.getLocalInstance(ctx).getMachineInfo(new ObjectUuidPK(machineid));
			if (machine.getMachineType() == null )
				throw(new Exception("考勤机没有设置型号!"));
			MachineTypeInfo machineType = MachineTypeFactory.getLocalInstance(ctx).getMachineTypeInfo(new ObjectUuidPK(machine.getMachineType().getId().toString()));
			if (machineType.getJavaclass() == null || machineType.getJavaclass().length() == 0)
			{
				throw(new Exception("考勤机类型没有配置读取方式!"));
			}
			IMachineFetch machineFetch = (IMachineFetch) Class.forName(machineType.getJavaclass()).newInstance();
			HashMap params = new HashMap();
			if (machine.getInstanceName() !=null)
				params.put("InstanceName", machine.getInstanceName());
			if (machine.getIpaddress() != null )
				params.put("Ipaddress", machine.getIpaddress() );
			if(machine.getOtherUrl1() != null )
				params.put("OtherUrl1", machine.getOtherUrl1() );
			if(machine.getOtherUrl2() != null )
				params.put("OtherUrl2", machine.getOtherUrl2() );
			if(machine.getOtherUrl3() != null )
				params.put("OtherUrl3", machine.getOtherUrl3());
			if(machine.getOtherUrl4() != null )
				params.put("OtherUrl4", machine.getOtherUrl4() );
			if (machine.getPassword() != null )
				params.put("Password", machine.getPassword());
			params.put("port",new Integer(machine.getPort()));

			if (machine.getUsername() != null )
				params.put("Username", machine.getUsername() );
			ArrayList arRet = machineFetch.FetchRecords(startDate, endDate, params);
			updateCardRecords(ctx,arRet,machineid);

		}
		catch(Exception ex)
		{
			throw(new BOSException(ex.getMessage()));
		}
		return super._fetchRecords(ctx, machineid, startDate, endDate, isOnline);
	}
	protected ArrayList updateCardRecords(Context ctx,ArrayList ar,String machineid) throws Exception
	{
		try
		{
			HashMap emps = new HashMap();
			Iterator it = ar.iterator();
			while(it.hasNext())
			{
				SimpleCardRecord card = (SimpleCardRecord)it.next();
				CardRecordInfo info = new CardRecordInfo();
				if (!emps.containsKey(card.eEmpID))
				{
					String tmp = getEmpIDFromNumber(ctx,card.eEmpID);
					if (tmp != null && tmp.length() >0)
					{
						emps.put(card.eEmpID, tmp);
					}
				}
				if (emps.containsKey(card.eEmpID))
				{
					PersonInfo person = new PersonInfo();
					person.setId(BOSUuid.read((String)emps.get(card.eEmpID)));
					info.setEmp(person);
					info.setBizDate(card.sDate);
					info.setCardType(TimeCardTypeEnum.NORMAL);
					Time t = Time.valueOf(card.sTime+ ":00");
					info.setBizTime(t);
					this._save(ctx, info);
				}
				else
				{
					continue;
				}
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw(ex);
		}
		return null;
	}
	protected String getEmpIDFromNumber(Context ctx,String number)
	{
		try
		{
			String strSQL = "Select FID from t_bd_person where FNumber='" + number+ "'";
			//String strSQL = "Select FID from t_bd_person where fname_l2='" + number+ "'";
			IRowSet iRowSet = DbUtil.executeQuery(ctx, strSQL);
			if (iRowSet.next())
			{
				return iRowSet.getString("FID");
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return null;
	}
}